package com.xy.mainp.huaban.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xy.debug.SystemConfig;
import com.xy.mainp.R;
import com.xy.mainp.huaban.utils.SearchUtil;
import com.xy.mainp.model.huaban.SearchHistoryEntity;
import com.xy.view.StandardAdapter;


import java.util.List;

/**
 * Created by John on 2016/11/16.
 */

public class HistoryPopup extends PopupWindow {

    private Context context;
    private View view;
    private ListView lv;
    private Button btn;
    private HistoryAdapter adapter;

    public HistoryPopup(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.view_history_popup, null);
        setContentView(view);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置弹出窗体可点击
        //this.setFocusable(true);  //会使editText失去焦点
        this.setOutsideTouchable(true);
        setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);  //设置弹出窗体需要软键盘
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        //刷新状态
        this.update();
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        initView();
    }

    private void initView() {
        lv = (ListView) view.findViewById(R.id.lv_history_popup);
        btn = (Button) view.findViewById(R.id.btn_history_popup);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SearchUtil.getInstance(context).clearData();
                dismiss();
            }
        });
        adapter = new HistoryAdapter();
        lv.setAdapter(adapter);
    }

    public void setItemClick(AdapterView.OnItemClickListener listener){
        lv.setOnItemClickListener(listener);
    }

    public void setData(List<SearchHistoryEntity> entities) {
        adapter.setData(entities);
        adapter.notifyDataSetChanged();
    }


    public void show(View anchor) {
        setWidth(anchor.getMeasuredWidth());
        showAsDropDown(anchor);

    }

    class HistoryAdapter extends StandardAdapter {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                TextView tv = new TextView(parent.getContext());
                tv.setPadding(SystemConfig.dp2px(context, 4), 0, 0, 0);
                tv.setTextColor(context.getResources().getColor(R.color.white_80));
                tv.setTextSize(16);
                tv.setBackgroundResource(R.color.blue_80);
                convertView = tv;
            }
            TextView tv = (TextView)convertView;
            tv.setText(((SearchHistoryEntity)getItem(position)).getText());
            return tv;
        }

    }
}
